package com.csw.android.agorademo.ui.main

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.csw.android.agorademo.R
import com.csw.android.agorademo.databinding.ItemMainFunctionListBinding
import com.csw.android.agorademo.databinding.UiMainBinding
import com.csw.android.agorademo.ui.video_call.VideoCallFragment
import com.csw.android.agorademo.ui.voice_call.VoiceCallFragment
import com.csw.android.dev_utils.ex.ActivityEx.openInCommonActivity
import com.csw.android.dev_utils.ui.databinding.DataBindingActivity
import com.csw.android.dev_utils.ui.databinding.DataBindingViewHolder

class MainActivity : DataBindingActivity<UiMainBinding>() {
    override fun getContentViewID() = R.layout.ui_main

    override fun initView(rootView: View, savedInstanceState: Bundle?) {
        super.initView(rootView, savedInstanceState)
        dataBinding?.run {
            recyclerView.run {
                layoutManager = LinearLayoutManager(context)
                adapter = FunctionAdapter()
            }
        }
    }

    private inner class FunctionItem<F : Fragment>(val name: String, val fragmentClass: Class<F>)

    private inner class FunctionAdapter :
        BaseQuickAdapter<FunctionItem<*>, DataBindingViewHolder<ItemMainFunctionListBinding>>(
            R.layout.item_main_function_list
        ) {

        init {
            setList(
                listOf(
                    FunctionItem(
                        getString(R.string.main_fun_voice_call),
                        VoiceCallFragment::class.java
                    ),
                    FunctionItem(
                        getString(R.string.main_fun_video_call),
                        VideoCallFragment::class.java
                    ),
                )
            )
            setOnItemClickListener { _, _, position ->
                openInCommonActivity(getItem(position).fragmentClass)
            }
        }

        override fun convert(
            holder: DataBindingViewHolder<ItemMainFunctionListBinding>,
            item: FunctionItem<*>
        ) {
            holder.dataBinding.run {
                name.text = item.name
            }
        }

    }
}